#include "StdAfx.h"
#include "DatabaseAdaptor.h"

//#include <fstream>

CDatabaseAdaptor* CDatabaseAdaptor::s_instance = NULL;


CDatabaseAdaptor::CDatabaseAdaptor(CResipTesterDlg& dlg): m_dlg(dlg)
{
}

CDatabaseAdaptor::~CDatabaseAdaptor(void)
{
}


CDatabaseAdaptor* CDatabaseAdaptor::getInstance(CResipTesterDlg* dlg)
{
	if(!s_instance && dlg){
		s_instance = new CDatabaseAdaptor(*dlg);
	}
	return s_instance;
}


bool CDatabaseAdaptor::existDownHost(std::string& host){
    bool rt = false;
    int i = m_dlg.getDownHost().find(host);
    if (i >= 0){
        rt = true;
    }
    return rt;
}


bool CDatabaseAdaptor::existUpHost(std::string& host){
    bool rt = false;
    int i = m_dlg.getUpHost().find(host);
    if (i >= 0){
        rt = true;
    }
    return rt;
}


bool CDatabaseAdaptor::existUp(){
    return (m_dlg.getUpHost().length() > 0);
}


bool CDatabaseAdaptor::existDown(){
    return (m_dlg.getDownHost().length() > 0);
}


bool CDatabaseAdaptor::registeredHost(std::string& host)
{
    bool rt = false;
    std::list<std::string>::iterator it;
    it = m_regHostList.begin();
    for (; it != m_regHostList.end(); it++){
        int i = host.find(*it);
        if (i >= 0){
            rt = true;
            break;
        }
    }
    return rt;
}


bool CDatabaseAdaptor::pushRegHost(std::string& host){
    m_regHostList.push_back(host);
    return true;
}


std::string CDatabaseAdaptor::queryDevice(std::string& devName)
{    
    if (canForceFail()){
        std::string none;
        return none;
    } else if (canB2bDispatch() && existDown()){
        return m_dlg.getSipUriDown();
    } else {
        return std::string("localhost");
    }
}


//std::string CDatabaseAdaptor::querySipDown(){
//	std::string sipHost;
//	if (BST_CHECKED == m_dlg.m_b2bCheck.GetCheck()){
//		sipHost = m_dlg.getSipUriDown();
//	} else {
//		sipHost = "localhost";
//	}
//	return sipHost;
//}
//
//
//std::string CDatabaseAdaptor::queryDown(){
//	std::string sipHost;
//	if (BST_CHECKED == m_dlg.m_b2bCheck.GetCheck()){
//		sipHost = m_dlg.getDownHost();
//	} else {
//		sipHost = "localhost";
//	}
//	return sipHost;
//}
//
//
//std::string CDatabaseAdaptor::queryUp()
//{
//	std::string sipHost;
//	if (BST_CHECKED == m_dlg.m_b2bCheck.GetCheck()){
//		sipHost = m_dlg.getUpHost();
//	} else {
//		sipHost = "localhost";
//	}
//	return sipHost;
//}


bool CDatabaseAdaptor::canForceFail(){
	return (BST_CHECKED == m_dlg.m_FailCheck.GetCheck());
}


bool CDatabaseAdaptor::canB2bDispatch(){
    return (BST_CHECKED == m_dlg.m_b2bCheck.GetCheck());
}


